package cn.tedu.yitu.password.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import cn.tedu.yitu.common.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class CaptchaController {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @GetMapping("/captcha")
    public JsonResult generateCaptcha(){
        GifCaptcha gifCaptcha = CaptchaUtil.createGifCaptcha(100,30,4);
        String captchaKey = UUID.randomUUID().toString();
        String cacheKey = "captcha:" + captchaKey;
        redisTemplate.opsForValue().set(cacheKey,gifCaptcha.getCode(),2, TimeUnit.MINUTES);
        Map<String, Object> data = new HashMap<>();
        data.put("captchaKey",captchaKey);
        data.put("captchaBase64", gifCaptcha.getImageBase64Data());
        return JsonResult.ok(data);
    }
}
